
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
const app = getApp();
Page({
    data: {
        orderAll: [], //订单信息
        o_id: 0, //默认订单id
        fileList: [], //图片数组
        imgMax: 1048576 * 2, //单图片大小
        activeNames: [],
        sum_price: 0, //计算订单总价
        driver: [], //登录人员信息
        buttstatus: 1, //点击提交按钮禁用
        controlUpload: true, //控制文件上传
        imgDelete: true, //控制文件删除
        remark: '',

        view_delivery_man_id: 0,
        view_driver_name: '',
        view_delivery_district_id: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

        let view_delivery_man_id = wx.getStorageSync('view_delivery_man_id') || 0
        let view_driver_name = ''
        let view_delivery_district_id = 0
        if (view_delivery_man_id > 0) {
            view_driver_name = wx.getStorageSync('view_driver_name')
            view_delivery_district_id = wx.getStorageSync('view_delivery_district_id')
        }

        this.setData({
            view_delivery_man_id,
            view_driver_name,
            view_delivery_district_id
        })

        let that = this
        let sum_price = 0
        let remark = ''
        //接收上级传递的参数
        const eventChannal = this.getOpenerEventChannel() //使用const 声明的不能被修改，必须立刻赋值
        eventChannal.on('orderAll', function (res) {
            let response = JSON.parse(res)  //传值过来是json 需要转换对象
            let orderAll = response.orderAll
            console.log('传过来的', orderAll)

            for (let i = 0; i < orderAll.length; i++) {
                sum_price += parseFloat(orderAll[i]['total_pay_price'])
                let order_remark = orderAll[i]['remark']
                if (order_remark.length > 0) {
                    remark += order_remark + '\n'
                }
                console.log('sum_price:', sum_price, remark)
            }


            that.setData({
                orderAll: orderAll,
                o_id: response.id,
                sum_price: sum_price,
                remark: remark
            })
        })
        /** 在缓存中读取当前账号登录信息 */
        let delivery_man = wx.getStorageSync('delivery_man') 
        that.setData({
            delivery_man
        })
    },


    /* 上传文件查看图片大小 */
    imgSize(event) {
        let imgMax = this.data.imgMax; //获取默认上传文件大小
        const {
            file
        } = event.detail; //获取上传文件数组
        for (let i = 0; i < file.length; i++) {
            if (file[i].size > imgMax) {
                Toast('大小超过2MB~')
            }
        }
    },
    /* 上传文件后事件，处理照片放入OSS回显 */
    afterRead(event) {
        let that = this
        const {
            file
        } = event.detail; //获取上传文件数组
        //ajax处理照片
        for (let i = 0; i < file.length; i++) {
            /* 开始上传 */
            wx.uploadFile({
                url: app.globalData.URL + '/deliver.Upload/uploadImg',
                filePath: file[i]['url'], //传递文件
                name: 'image', //获取照片信息的名称
                success(res) {
                    let result = JSON.parse(res.data) //传过来的是json 需要转换成对象
                    Toast.success('上传成功');
                    /* 填充到 */
                    // 上传完成需要更新 fileList
                    const {
                        fileList = []
                    } = that.data;
                    fileList.push({
                        url: result.data
                    })
                    that.setData({
                        fileList, 
                    });
                }
            })
            
          
        }
       
    },
    /**
     * 删除上传的照片
     */
    imgDelete(event) {
        let that = this;
        let urlPath = event.detail.file.url
        wx.request({
            url: app.globalData.URL + '/deliver.Upload/deleteImg',
            method: 'DELETE',
            header: {
            },
            data: {
                path: urlPath
            },
            success: res => {
                if (res.data.code === 1) {
                    Toast('删除图片失败');
                } else {
                    const {
                        fileList = []
                    } = that.data;
                    fileList.splice(event.detail.index, 1)
                    that.setData({
                        fileList
                    });
                    Toast('删除成功');
                }
            }
        })
    },
    /**
     * 信息提交
     */
    async bindingOrder(event) {
        this.setData({
            buttstatus: 0, //点击绑定，按钮不能点击
            controlUpload: false, //控制文件上传
            imgDelete: false, //控制文件删除
        })
        let img = this.data.fileList //获取上传图像
        let o_id = this.data.o_id //获取上传图像
        let mall_id = app.globalData.mall_id //获取主商城id

        let remark = this.data.remark
        if (remark.length > 0) {
            await Dialog.alert({
                title: '请注意商户留言:',
                message: remark,
                showConfirmButton: true,
            }).then(() => {
                // on close
            });
        }

        wx.request({
            url: app.globalData.URL + '/deliver.OrderV2/bindingOrder',
            method: 'POST',
            data: {
                bindingOrder: event.detail.value,
                img,
                mall_id,
                o_id,

            },
            success: res => {
                if (res.data.code === 0) {
                    Toast('绑定成功~~')
                    wx.reLaunch({
                        url: '/templates/deliver/pages/index/index',
                    })
                }else{
                    Toast('绑定失败~~')
                    wx.reLaunch({
                        url: '/templates/deliver/pages/index/index',
                    })
                }
            }
        })
    },
    /**
     * 默认打开折叠
     */
    onChange(event) {
        this.setData({
            activeNames: event.detail,
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        console.log('6666')
        /* 设置手风琴，设置订单全部展开 */
        let that = this
        let sumJ = 0
        let le = this.data.orderAll
        for (let i = 0; i < le.length; i++) {
            sumJ += Number(le[i].total_pay_price) //计算总金额
            const {
                activeNames = []
            } = that.data
            //activeNames.push(i)
            that.setData({
                //activeNames,
                sum_price: sumJ
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})